﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 必须大于或等于某个值, 如:
/// <code>
/// public class Person
/// {
///     [VMustGreaterThanOrEuqalTo(60)]
///     public double Score { get; set; }
/// }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustGreaterThanOrEuqalToAttribute(object value, string errorMessage = null) : Attribute, IValidateAction
{
    public object Value { get; } = value;
    public string ErrorMessage { get; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateExtensions.MustGreaterThanOrEuqalToInternal(ctx, Value, ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则必须大于或等于某个值, 如:
/// <code>
/// public class Person
/// {
///     [VMustGreaterThanOrEuqalToIfNotNull(60)]
///     public double Score { get; set; }
/// }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustGreaterThanOrEuqalToIfNotNullAttribute(object value, string errorMessage = null) : Attribute, IValidateAction
{
    public object Value { get; } = value;
    public string ErrorMessage { get; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateExtensions.MustGreaterThanOrEuqalToInternal(ctx, Value, ErrorMessage);
    }
}